package com.heima.user.service.impl;

import com.heima.common.exception.CustException;
import com.heima.model.common.constants.user.UserRelationConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.dtos.UserRelationDTO;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.service.ApUserRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class ApUserRelationServiceImpl implements ApUserRelationService {
    @Resource
    RedisTemplate<String, String> redisTemplate;

    @Override
    public ResponseResult follow(UserRelationDTO dto) {
        // 1. 校验参数    authorApUserId   必须登录   operation 0  1
        if (dto.getAuthorApUserId() == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "作者对应的userId不存在");
        }
        Short operation = dto.getOperation();
        if (operation == null || (operation.intValue() != 0 && operation.intValue() != 1)) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "关注类型错误");
        }
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer loginId = user.getId();
        Integer followId = dto.getAuthorApUserId();
        // 判断 自己不可以关注自己
        if (loginId.equals(followId)) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW, "不可以自己关注自己哦~");
        }
        //    校验之前有没有关注过   zscore
        //    参数1： key   参数2: 要查询集合元素
        Double score =
                redisTemplate.opsForZSet().
                        score(UserRelationConstants.FOLLOW_LIST + loginId, String.valueOf(followId));
        if (score != null && operation.intValue() == 0) {
            CustException.cust(AppHttpCodeEnum.DATA_EXIST, "你应经关注过哟");
        }
        //判断是0还是1
        try {
            if (operation.intValue() == 0) {
                //没有关注过得 zadd follow：我的id 作者id
                //    参数1 key 参数2 要查询的集合元素
                redisTemplate.opsForZSet().add(UserRelationConstants.FOLLOW_LIST
                        + loginId, String.valueOf(followId), System.currentTimeMillis());
                //            zadd fans z作者id 我的id
                redisTemplate.opsForZSet().add(UserRelationConstants.FANS_LIST+followId,
                        String.valueOf(loginId),System.currentTimeMillis());
            }else {
                //是1 取关
                //zrem follow： 我的id 作者id
                redisTemplate.opsForZSet().remove(UserRelationConstants.FOLLOW_LIST+loginId,String.valueOf(followId));
                redisTemplate.opsForZSet().remove(UserRelationConstants.FANS_LIST+followId,String.valueOf(loginId));
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 如果有异常   取消事务
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.okResult();
    }
}
